package org.springblade.upgrade.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.upgrade.config.entity.PackageBackendConfig;
import org.springblade.upgrade.config.mapper.PackageBackendConfigMapper;
import org.springblade.upgrade.config.model.PackageBackendConfigBean;
import org.springblade.upgrade.config.service.IPackageBackendConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Version 1.0
 */
@Service
public class PackageBackendConfigServiceImpl extends BaseServiceImpl<PackageBackendConfigMapper, PackageBackendConfig> implements IPackageBackendConfigService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addData(PackageBackendConfigBean param) throws Exception {
        //保存
        PackageBackendConfig entity = param;
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(PackageBackendConfigBean param) throws Exception {
        //更新
        PackageBackendConfig newEntity = param;
        return this.updateById(newEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(PackageBackendConfigBean param) throws Exception {
        if (param.getIdList() != null && !param.getIdList().isEmpty()) {
            UpdateWrapper<PackageBackendConfig> del = new UpdateWrapper<>();
            del.lambda().in(PackageBackendConfig::getId, param.getIdList());
            return this.remove(del);
        }
        if (param.getId() != null) {
            return this.removeById(param.getId());
        }
        return true;
    }

    @Override
    public PackageBackendConfigBean findById(Long id) {
        PackageBackendConfigBean cond = new PackageBackendConfigBean();
        cond.setId(id);
        return findBySpec(cond);
    }

    @Override
    public PackageBackendConfigBean findBySpec(PackageBackendConfigBean param) {
        List<PackageBackendConfigBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<PackageBackendConfigBean> findListBySpec(PackageBackendConfigBean param) {
        return this.baseMapper.queryList(param);
    }

    @Override
    public IPage<PackageBackendConfigBean> findPageBySpec(IPage<PackageBackendConfigBean> page, PackageBackendConfigBean param) {
        List<PackageBackendConfigBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }

}

